package com.cndw.kungfu.model.battle;

import com.cndw.kungfu.ext.NumberProcesser;

/**
 * 战斗的一些固定数值算法
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-7-11 下午4:40:14
 */
public class BattleFormula {
	/**
	 * 获取玩家的攻速,单位毫秒
	 * 
	 * @param attackSpeed
	 * @return
	 */
	public static int getSpeed(int attackSpeed) {
		attackSpeed = attackSpeed + 1;
		float a = attackSpeed == 0 ? 0 : 12000f / attackSpeed;
		float b = (float) (Math.round(a * 10)) / 10 * 1000;
		int speed = Float.valueOf(b).intValue();
		speed = NumberProcesser.between(speed, 2000, 8000);
		return speed;
	}

	/**
	 * 判断是否命中
	 * 
	 * @param source
	 * @param targer
	 * @return
	 */
	public static boolean checkHit(Fighter source, Fighter targer) {

		float a = 1910f * targer.getLevel() + 3000f;
		float b = 1910f * source.getLevel() + 3000f;

		float hitRate = 0.9f + source.getHit() / a - targer.getDodge() / b;

		int finalHitRate = Float.valueOf(hitRate * 100).intValue();
		if (source.getScoreCHK() > 0 && targer.getScoreCHK() > 0) {//战力压制
			int rateAdd = (source.getScoreCHK() - targer.getScoreCHK()) / 1500;
			finalHitRate += NumberProcesser.between(rateAdd, 0, 20);
			//System.out.println(source.getName() + "->  "+finalHitRate + " : add "+rateAdd);
		}
		finalHitRate = NumberProcesser.between(finalHitRate, 30, 100);

		return NumberProcesser.isWinning(finalHitRate);

	}

	/**
	 * 判断是否暴击
	 * 
	 * @param source
	 * @return
	 */
	public static boolean checkCrit(Fighter source, Fighter target) {

		float critRate = source.getCrit() / (1605f * target.getLevel() + 5000);
		float toughRate = target.getTough() / (1605f * source.getLevel() + 5000);

		int finalCritRate = Float.valueOf((0.05f + critRate - toughRate) * 100).intValue();
		finalCritRate = NumberProcesser.between(finalCritRate, 10, 70);
		if (source.getScoreCHK() > 0 && target.getScoreCHK() > 0) {//战力压制
			int rateAdd = (target.getScoreCHK() - source.getScoreCHK()) / 1500;
			finalCritRate -= NumberProcesser.between(rateAdd, 0, 20);
		}
		
		return NumberProcesser.isWinning(finalCritRate);
	}

	/**
	 * 计算伤害值
	 * 
	 * @param source
	 * @param targer
	 * @return
	 */
	public static float clacHurt(Fighter source, Fighter targer, int skillAttack) {
		float coefficient = 100f + 99 * source.getLevel();
		int finalAttack = NumberProcesser.mtRand(source.getAttack() * 9 / 10, source.getAttack() * 11 / 10);
		float hurt = finalAttack / (1f + targer.getDefense() / 10 / coefficient);
		hurt = hurt * skillAttack / 100;
		return hurt;
	}

	public static int clacExp(int exp, int monsterLevel, int playerLevel) {
		int finalExp = exp;
		if (playerLevel > monsterLevel) {
			finalExp = exp * (100 - (playerLevel - monsterLevel) * 10) / 100;
			finalExp = finalExp < 1 ? 1 : finalExp;
		}
		return finalExp;
	}

}
